-- LittleBoy
-- Created by chengb Aug/2/2016
-- 小男孩

-- 初始化
function initLittleBoy(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local progress = BraveVSDragonM.getLittleBoyProgress();
            local laijiaClass = DungeonM.getClassByType(GRID_TYPE_LAIJIA_GRAVE);

            if progress == 0 then
                -- 首次触发
                local function okFunc()
                    local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");
                    local classId = elementDbase["submit_item"];

                    if type(classId) == 'number' then
                        if ItemM.getAmount(ME.user, classId) <= 0 then
                            -- 缺少物品
                            alert(getLocStr("lack_submit_item"));
                            return;
                        end
                    end

                    if DungeonActionM.go("little_boy", self.gridData:getPos()) then
                        --alert(getLocStr("little_boy_tip1"));
                        local function okFunc2()
                            DungeonActionM.go("little_boy", self.gridData:getPos());
                        end

                        AudioM.playFx("ui_open1");
                        self:openBuildingView(okFunc2, nil, buildingInfo["dialog2"], buildingInfo["view_text2"]);
                    end
                end

                AudioM.playFx("ui_open1");
                self:openBuildingView2(okFunc, nil, buildingInfo["view_text"],
                    getLocStr("btn_text_leave"), buildingInfo["dialog"], nil, true);
            elseif not BraveVSDragonM.hasGotBuildingBonus(laijiaClass) then
                -- 未领取莱迦之墓的奖励
                local function okFunc()
                    DungeonActionM.go("little_boy", self.gridData:getPos());
                end
                if progress == 1 then
                    AudioM.playFx("ui_open1");
                    self:openBuildingView(okFunc, nil, buildingInfo["dialog2"], buildingInfo["view_text2"]);
                else
                    AudioM.playFx("ui_open1");
                    self:openBuildingView(nil, nil, buildingInfo["dialog2"], buildingInfo["view_text2"]);
                end
            else
                alert(getLocStr("little_boy_tip2"));
            end
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setVisible(true);

    -- 呼吸
    self.needAnimation = true;
end
